<?php
require_once 'models/user_model.php';
class Login_Controller 
{
	public $model; 
	public $action = "index";
	public $error = false;
	
	public function  Login_Controller() 
	//public function  __construct() 
	{
		//parent::__construct();
		//echo "<pre>";var_dump(array('a', 'b', 'c'));	exit;
		if(isset($_GET["action"])) $this->action = $_GET["action"];
		$this->{$this->action}();
	}

	public function index() 
	{
		session_start();

		if(isset($_SESSION['useremail'])) {
			Header("location: index.php?controller=home");
		}
		if (isset($_POST['email'])){
			$email=$_POST['email'];
			$pass=md5($_POST['pass']);
			$this->model = new User_Model();
			if($this->model->checkUser($email, $pass) ) {
				$_SESSION['useremail']=$email;
				$_SESSION['id']=$this->model->getId($email);
                $duty=$this->model->getduty($email);
				if ($duty=='admin'){
				Header("location: index.php?controller=home");
				}
                else Header("location: index.php?controller=pagehome&action=lists");

			} else {
				$this->error = "Wrong useremail or pass";
				include "views/login/login.php";
			}
		} else {
			include "views/login/login.php";
		}
	}
	
	public function logout() 
	{
		$this->model = new User_Model();
		session_start();
	    session_destroy();
		Header("location: index.php?controller=login");
	}
}
?>

